/**
 * 文件名称:  BaseCarController.java
 * 文件序号:  序号
 * 内容描述:  文件简单描述，多行换行书写
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  zhangkai
 * 创建日期:  2017/2/27 14:16
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */

package com.hongdee.crm.base.controller;


import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.StringUtil;

import com.hongdee.crm.base.service.ICarZhService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * 名  称:BaseCarController
 * 类  型: class
 * 描  述:  组合查询
 * 路  径:com.hongdee.crm.base.controller
 * 作  者:zhangkai
 * 日  期:2017/2/27 14:16
 */
@RequestMapping("/crm/car/zh")
@RestController
public class BaseCarZhController {
    @Autowired
    private ICarZhService carZhService;

    /**
     * 分页查询基盘车辆
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map params) throws Exception {
        Map pageMap = carZhService.page(start, limit, params, uid);
        pageMap.put("success", true);
        pageMap.put("msg", "组合分页查询成功！");
        return pageMap;
    }

    /**
     * 按查询条件导出组合查询的车辆信息
     **/
    @RequestMapping("/exportExecl")
    @ResponseBody
    public ResultEntity exportExecl(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    HttpServletRequest request, HttpServletResponse response,
                                    @RequestBody Map params) throws Exception {

        List dataList = this.carZhService.excelZH(params, uid);
        System.out.println(params);
        XSSFWorkbook wb = this.carZhService.createExcel(dataList, uid);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
        path =   path + File.separator + "upload";

        File filepack = new File(path);
        if(!filepack.exists()){
            filepack.mkdir();
        }
        String filePath =   path + File.separator + fileId + ".xlsx";
        System.out.println("文件路径：" + filePath);
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }

}
